package com.kiln.juxingqs.ui

import android.content.Intent
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.lifecycle.Observer
import client.global.commlib.AppConfig_c
import client.global.commlib.toast.toast
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.ActivityLoginBinding
import com.kiln.juxingqs.ui.vm.LoginViewModel

class LoginActivity : BaseMiddleActivity<LoginViewModel, ActivityLoginBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_login
    }

    override fun initView() {
        binding?.apply {
            vm = viewModel
            login = this@LoginActivity
        }
    }

    override fun observerUI() {
        viewModel.auth.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("登录成功")
                    if (data!!.dg_status == 1) {
                        startActivity(
                            Intent(
                                this@LoginActivity,
                                RealAbleDateVerifyActivity::class.java
                            ).apply {
                                putExtra("isLogin", true)
                            }
                        )
                    } else {
//                        startActivity(Intent(this@LoginActivity, UpGradeActivity::class.java))
                        startActivity(Intent(this@LoginActivity, MainActivity::class.java))
                    }
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
        viewModel.isShowPwd.observe(this, Observer {
            if (it) {
                binding!!.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding!!.etPwd.setSelection((binding!!.etPwd.getText().toString().trim()).length);
            } else {
                binding!!.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding!!.etPwd.setSelection((binding!!.etPwd.getText().toString().trim()).length);
            }
        })
    }

    override fun initViewModel(): LoginViewModel = createViewModel()

    fun lookxy() {
        startActivity(Intent(this, WebActivity::class.java).apply {
            putExtra("adInfo", "http://139.224.230.84/d/greement.html")
            putExtra("title", "交易规则协议")
        })
    }
}